package com.bawei.module_home.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_home.adapter.AreaAdapter.AreaViewHolder
import com.bawei.module_home.databinding.ItemAreaBinding
import com.bawei.module_home.databinding.ItemNewsBinding
import com.bawei.module_home.model.AreaModelItem
import com.bawei.module_home.model.NewsModelItem

class NewsAdapter: RecyclerView.Adapter<NewsAdapter.NewsViewHolder>() {
    val list=mutableListOf<NewsModelItem>()

    fun setData(data:List<NewsModelItem>){
        list.clear()
        list.addAll(data)
        notifyDataSetChanged()
    }
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): NewsViewHolder {
        val binding= ItemNewsBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )

        return NewsViewHolder(binding)
    }

    override fun onBindViewHolder(
        holder: NewsViewHolder,
        position: Int
    ) {
        val binding=DataBindingUtil.getBinding<ItemNewsBinding>(holder.itemView) ?:return
        binding.name.text=list[position].title
        binding.content.text=list[position].content
        binding.time.text=list[position].publishDate

    }


    override fun getItemCount(): Int {
        return list.size
    }

    class NewsViewHolder(item: ItemNewsBinding): RecyclerView.ViewHolder(item.root)
}